package utilx

import (
	"github.com/jinzhu/copier"
	"math/rand"
	"strings"
	"time"
)

// StructMapToStruct 结构体映射
func StructMapToStruct(s, ts interface{}) error {
	return copier.Copy(ts, s)
}

// StructMapToStruct 结构体映射
func MapToStruct(m, s interface{}) error {
	data, err := json.Marshal(m)
	if err != nil {
		return err
	}
	return Unmarshal(data, s)
}
func GetTypeByFileName(filename string) string {
	n := strings.LastIndexByte(filename, '.')
	if n != 0 {
		return filename[n:]
	}
	return ""
}

//RandomStr 随机生成字符串
func RandomStr(length int) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyz"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}
